package com.mineplatform.service;

import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mineplatform.dao.M_RoleDao;
import com.mineplatform.dao.M_UserDao;
import com.mineplatform.dao.M_UserPraiseDao;
import com.mineplatform.domain.M_User;
import com.mineplatform.domain.M_UserPraise;
import com.mineplatform.exception.FromUserNotExistException;
import com.mineplatform.exception.ToUserNotExistException;
import com.mineplatform.iservice.IM_UserPraiseService;

@Service
public class M_UserPraiseService implements IM_UserPraiseService {

	private M_UserPraiseDao _m_userpraisedao;
	private  M_UserDao _m_userdao;

	@Autowired
	public M_UserPraiseService(M_UserPraiseDao m_userpraisedao,M_UserDao m_userdao) {
		this._m_userpraisedao = m_userpraisedao;
		this._m_userdao = m_userdao;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void praiseuser(String from, String to) throws FromUserNotExistException, ToUserNotExistException {
		// TODO Auto-generated method stub

		M_User fromUser = this._m_userdao.findById(from);
		if (fromUser == null) {

			throw new FromUserNotExistException();
		}
		M_User toUser = this._m_userdao.findById(to);
		if (toUser == null) {

			throw new ToUserNotExistException();
		}

		toUser.setPraiseCount(toUser.getPraiseCount()+1);
		
		
		this._m_userdao.save(toUser);
		
		M_UserPraise p=new M_UserPraise();
		p.setCreateTime(new Date());
		p.setId(UUID.randomUUID().toString());
		p.setFrom(fromUser.getId());
		p.setTo(toUser.getId());
		
	    this._m_userpraisedao.save(p);
		
	}

	@Override
	public Boolean ispraiseuser(String from, String to) throws FromUserNotExistException, ToUserNotExistException {
		// TODO Auto-generated method stub
		M_User fromUser = this._m_userdao.findById(from);
		if (fromUser == null) {

			throw new FromUserNotExistException();
		}
		M_User toUser = this._m_userdao.findById(to);
		if (toUser == null) {

			throw new ToUserNotExistException();
		}
		return this._m_userpraisedao.ispraiseuser(from,to);
		
	}

}
